<template>
<div>
  <el-breadcrumb separator-class="el-icon-arrow-right">
    <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
    <el-breadcrumb-item>营销管理</el-breadcrumb-item>
    <el-breadcrumb-item>客服问答</el-breadcrumb-item>
  </el-breadcrumb>
  <el-card class="box-card">
    <el-row :gutter="20">
      <el-col :span="7">
        <el-input placeholder="请输入所属模块" v-model="query">
          <el-button slot="append" icon="el-icon-search" @click="search">搜索</el-button>
        </el-input>
      </el-col>
      <el-col :span="4">
        <el-button type="primary" @click="addCustomerIssues" >新增</el-button>
      </el-col>
      <el-col :span="7">
        <el-button type="primary" @click="shuaxin">刷新</el-button>
      </el-col>
    </el-row>
    <!--  表格 -->
    <template>
      <el-table
          :data="tableData"
          border
          stripe
          style="width: 100%">
        <el-table-column
            fixed
            prop="id"
            label="id"
            width="50"

        >
        </el-table-column>
        <el-table-column
            prop="submodule"
            label="所属模块"
            width="200"

        >
        </el-table-column>
        <el-table-column
            prop="question"
            label="问题"
            width="200"

        >
        </el-table-column>
        <el-table-column
            prop="answer"
            label="答案"
            width="350"

        >
        </el-table-column>
        <el-table-column
            prop="addtime"

            label="添加时间"

        >
        </el-table-column>
        <el-table-column
            fixed="right"
            label="操作"
            align="center"
        >
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" @click="editCustomerIssues(scope.row)"  size="medium" circle></el-button>
            <el-button type="danger" icon="el-icon-delete"  @click="delCustomerIssues(scope.row)"  size="medium" circle></el-button>

          </template>

        </el-table-column>
      </el-table>
    </template>
    <!-- 分页 -->
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5,10,15,20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>

  </el-card>
  <!--修改用户信息-->
  <el-dialog
      title="修改用户"
      :visible.sync="editDialogVisible"
      width="50%"
      @close="closeEditcustmoer"
  >
    <el-form :model="customerIssuesEditFormData" status-icon :rules="customerIssuesEditFormRules" ref="customerIssuesEditRuleForm" label-width="100px">

      <el-form-item label="id" prop="id" hidden>
        <el-input type="text" v-model="customerIssuesEditFormData.id" autocomplete="off" ></el-input>
      </el-form-item>
      <el-form-item label="所属模块" prop="submodule">
        <el-input type="text" v-model="customerIssuesEditFormData.submodule" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="问题" prop="question">
        <el-input type="text" v-model="customerIssuesEditFormData.question" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="答案" prop="answer">
        <el-input type="text" v-model="customerIssuesEditFormData.answer" autocomplete="off"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
                <el-button @click="editDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="confirmEditCustomerIssues">确 定</el-button>
   </span>
  </el-dialog>

  <!--添加用户信息-->
  <el-dialog
      title="添加用户"
      :visible.sync="addeditDialogVisible"
      width="50%"
      @close="addcloseEditcustmoer"
  >
    <el-form :model="addcustomerIssuesEditFormData" status-icon :rules="addcustomerIssuesEditFormRules" ref="addcustomerIssuesEditRuleForm" label-width="100px">

      <el-form-item label="id" prop="id" hidden>
        <el-input type="text" v-model="addcustomerIssuesEditFormData.id" autocomplete="off" ></el-input>
      </el-form-item>
      <el-form-item label="所属模块" prop="submodule">
        <el-input type="text" v-model="addcustomerIssuesEditFormData.submodule" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="问题" prop="question">
        <el-input type="text" v-model="addcustomerIssuesEditFormData.question" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="答案" prop="answer">
        <el-input type="text" v-model="addcustomerIssuesEditFormData.answer" autocomplete="off"></el-input>
      </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
                <el-button @click="addeditDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="addconfirmEditCustomerIssues">确 定</el-button>
   </span>
  </el-dialog>


</div>
</template>

<script>
export default {
name: "question",
  data(){
   return{
     //查询条件
     query:"",
     //当前页码
     currentPage: 1,
     //每页显示记录
     pageSize:5,
     //总条数
     total:0,
    tableData: [],
     //修改表单的数据
     customerIssuesEditFormData:{},
     //添加表单的数据
     addcustomerIssuesEditFormData:{},
     //编辑弹框的显示或隐藏
     editDialogVisible:false,
     //添加弹框的显示或隐藏
     addeditDialogVisible:false,
     //编辑表单的校验规则
     customerIssuesEditFormRules:{

       question:[
         {required:true,message:"问题不能为空",trigger:"blur"},
       ],
       answer:[
         {required:true,message:"答案不能为空",trigger:"blur"},
       ],
       submodule:[
         {required:true,message:"答案不能为空",trigger:"blur"},
       ],

     },
     //添加表单的校验
     addcustomerIssuesEditFormRules:{
       question:[
         {required:true,message:"问题不能为空",trigger:"blur"},
       ],
       answer:[
         {required:true,message:"答案不能为空",trigger:"blur"},
       ],
       submodule:[
         {required:true,message:"答案不能为空",trigger:"blur"},
       ],
     },
     }
  },
  created() {
    this.initQuTable(this.currentPage,this.pageSize);
  },
  methods: {
    //刷新
    shuaxin(){
      this.query="";
      this.initQuTable(this.currentPage,this.pageSize);
    },

    //修改每页几条的方法
    handleSizeChange(val) {
      this.pageSize=val;
      this.initQuTable(this.currentPage,this.pageSize);
    },
    //修改页数的方法
    handleCurrentChange(val) {
      this.currentPage=val;
      this.initQuTable(this.currentPage,this.pageSize);
    },
    //搜索
    search() {
      this.initQuTable(this.currentPage, this.pageSize)
    },

    //分页查询的方法
    initQuTable(currentPage,pageSize){
      var that=this;
      this.$http.post(`/marketing/tbCustomerIssues/findQuestionByPage/${currentPage}/${pageSize}?query=${this.query}`).then(function(resp){
        if(resp.data.code===200){
          that.tableData=resp.data.data.records;
          that.total=resp.data.data.total;
        }else{
          that.$message.error("用户加载失败");
        }
      });
    },
    //删除
    delCustomerIssues(customerIssues){
      var that=this;
      this.$confirm('是否删除模块:'+customerIssues.submodule+'?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        that.$http.post(`/marketing/tbCustomerIssues/delCustomer/${customerIssues.id}`).then(function(res){
          if (res.data.code == 200){
            that.$message.success(res.data.msg);
            that.initQuTable(that.currentPage,that.pageSize);
          }
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });

    },
    //修改弹框回显的方法
    editCustomerIssues(row){
      this.editDialogVisible = true;
      this.customerIssuesEditFormData=row;
      //console.log(userId);
     /* var that = this;
      this.$http.get(`/marketing/tbCustomerIssues/getCustomerById/${row.id}`).then(function (res){
        that.customerIssuesEditFormData=res.data.data;
      })*/
    },
    //提交修改弹框的方法
    confirmEditCustomerIssues(){
      var that = this;
      this.$refs['customerIssuesEditRuleForm'].validate((valid) => {
        if (valid) {
          that.$http.put("/marketing/tbCustomerIssues/updateCustomer",this.customerIssuesEditFormData).then(function (res){
            if (res.data.code == 200){
              that.$message.success("修改成功");
              that.editDialogVisible=false;
              that.initQuTable(that.currentPage,that.pageSize);
            }else{
              that.$message.error("修改失败");
            }
          })
        }
      })
    },
    //添加的方法
    addCustomerIssues(){
      this.addeditDialogVisible = true;
      this.addcustomerIssuesEditFormData={};
      /*var that = this;
      this.$refs['addcustomerIssuesEditRuleForm'].validate((valid) => {
        if (valid) {
          that.$http.post("/marketing/tbCustomerIssues/addCustomer",this.addcustomerIssuesEditFormData).then(function (res){
            if (res.data.code == 200){
              that.$message.success("添加成功");
              that.addeditDialogVisible=false;
              that.initQuTable(that.currentPage,that.pageSize);
            }else{
              that.$message.error("添加失败");
            }
          })
        }
      })*/
    },
    addconfirmEditCustomerIssues(){
      var that = this;
     this.$refs['addcustomerIssuesEditRuleForm'].validate((valid) => {
       if (valid) {
         that.$http.put("/marketing/tbCustomerIssues/addCustomer",this.addcustomerIssuesEditFormData).then(function (res){
           if (res.data.code == 200){
             that.$message.success("添加成功");
             that.addeditDialogVisible=false;
             that.initQuTable(that.currentPage,that.pageSize);
           }else{
             that.$message.error("添加失败");
           }
         })
       }
     })
    },
    //提交添加弹框的方法

    //修改弹窗关闭时触发的事件
    closeEditcustmoer(){
      this.$refs.customerIssuesEditRuleForm.resetFields();
    },
    //添加弹窗关闭时触发的事件
    addcloseEditcustmoer(){
      this.$refs.addcustomerIssuesEditRuleForm.resetFields();
    },
   /* formatTime(row, column) {
      const date = new Date(row[column.property])
      return date.getFullYear() + '年' +
          date.getMonth() + '月' +
          date.getDate() + '日 ' +
          date.getHours()
          + ':' +date.getMinutes()
    },*/
  }
}
</script>

<style scoped>

</style>